/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto.voztg.pkg2.pkg0;

/**
 *
 * @author nascimento lima
 */



import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
// aprender import java.io.FilePermission;


/*import voztg.Text;
import voztg.Synthesizer;
import voztg.SynthesizerFactory;
import voztg.ComponentUtils;*/



public class FurbSpeech {
	
	private static final String DEFAULT_OUTPUT_FILENAME = "speech.wav";
	

	private String text;
	private File voiceFile;
	private String fileName;
	
	public FurbSpeech() {
		super();
	}
	
	/**
	 * Defines the text of the operation.
	 * @param text
	 * @return
	 */
	public FurbSpeech text(String text) {
		this.text = text;
		return this;
	}
	
	/**
	 * Defines the voice to be used by sinthesizer application.
	 * @param voiceFile
	 * @return
	 */
	public FurbSpeech withVoice(File voiceFile) {
		this.voiceFile = voiceFile;
		return this;
	}
	
	/**
	 * Defines the default output WAV filename.
	 * @return
	 */
	public FurbSpeech to() {
		fileName = DEFAULT_OUTPUT_FILENAME;
		return this;
	}
	
	/**
	 * Defines the output WAV filename.
	 * @param outputType
	 * @param fileName Relative to the user project binary directory.
	 * @return
	 */
	public FurbSpeech to(String fileName) {
		this.fileName = fileName.startsWith("/") ? fileName.substring(1) : fileName;
		return this;
	}
	
	/**
	 * Speech the text based on previous defined text, output file and voice.
	 */
	public File speech() {
		checkAttributes();
		
		Text texti = new Text(this.text);
		texti.parsePhrases(ComponentUtils.BASE_FREQUENCY, ComponentUtils.BASE_TIME);
		String output = texti.showPhrases();
		
		File synthInput = writeOutputInTheFile(output);
		 
		//File outputSpeechFile = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("output").getFile() + "/" + this.fileName));original
		File outputSpeechFile = new File("c:/recursos/output" + "/" + this.fileName);
                
		Synthesizer synthesizer = SynthesizerFactory.getSynthesizer();
		if (voiceFile != null) {
			synthesizer.setVoice(voiceFile);
		}
		synthesizer.synthesize(synthInput, outputSpeechFile);
		
		return outputSpeechFile;
	}

	private File writeOutputInTheFile(String output) {
		//FurbSpeech.getLogger().debug("Creating output directory");
		//File outputDir = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource(".").getFile()).concat("/output/"));original
		//File outputDir = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource(".").getFile()).concat("/output/"));
            //outputDir.mkdir();
		//File fileOutput = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("output").getFile()) + "/output.pho");original
		
          File fileOutput = new File("c:/recursos/output" + "/output.pho");  
            try {
			if (!fileOutput.exists()) {
				fileOutput.createNewFile();
				
			}
			FileWriter fw = new FileWriter(fileOutput);
			fw.write(output);
			fw.close();
			
			return fileOutput;
		}
		catch (IOException e) {
			System.out.println("I/O error writting .pho file");
			
			return null;
		}
	}
	
	private void checkAttributes() {
		if (this.text == null) {
			throw new IllegalStateException("The text must be previously defined. Before this invocation, call the method text(String)");
		}
	
		if (this.fileName == null) {
			throw new IllegalStateException("The output WAV file name must be previously defined. Before this invocation, call the method to() or to(String)");
		}
	}
	
	
}
